//
//  XHStautsCell.swift
//  gu-新浪微博
//
//  Created by xiaohui on 16/6/28.
//  Copyright © 2016年 gusheghui. All rights reserved.
//

import UIKit
import SnapKit
class XHStautsCell: UITableViewCell {
    
    var toolBarTopConstraint:Constraint?
    
    var status:XHStatus?{
        didSet{
            statuseOriginaView.status = status
            
            toolBarTopConstraint?.uninstall()
            
            if status?.retweeted_status == nil{
                //没有转发微博
                statuseToolBar.snp_updateConstraints(closure: { (make) -> Void in
                    statusRetweetView.hidden = true
                   toolBarTopConstraint = make.top.equalTo(statuseOriginaView.snp_bottom).constraint
                })
            }else{
                statuseToolBar.snp_updateConstraints(closure: { (make) -> Void in
                    //先赋值再更新约束 
                    statusRetweetView.status = status
                    
                    statusRetweetView.hidden = false
                    toolBarTopConstraint = make.top.equalTo(statusRetweetView.snp_bottom).constraint
                })
            }
            
            //给toolbar赋值
            statuseToolBar.status = status
            
        }
    }
  
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
  
    //懒加载StatusView
    private lazy var statuseOriginaView:XHStatusOriginalView = XHStatusOriginalView()
    //懒加载RetweetView
    private lazy var statusRetweetView:XHStatusRetweetView = XHStatusRetweetView()
    
    //懒加载tooBar
    private lazy var statuseToolBar:XHStatusToolBar = XHStatusToolBar()
    
    
    
    
    //设置cell UI
    private func setupUI(){
    
        self.backgroundColor = UIColor.whiteColor()
        contentView.addSubview(statuseOriginaView)
        contentView.addSubview(statusRetweetView)
        contentView.addSubview(statuseToolBar)
        statuseOriginaView.snp_makeConstraints { (make) -> Void in
            make.left.top.right.equalTo(contentView)
            
        }
        statuseToolBar.snp_makeConstraints { (make) -> Void in
            //卸载这条约束并    记录
           toolBarTopConstraint = make.top.equalTo(statusRetweetView.snp_bottom).constraint
            make.left.right.equalTo(contentView)
            make.height.equalTo(35)
        }
        
        //约束cell
        contentView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(statuseToolBar)
            make.right.left.top.equalTo(self)
        }
        
        
        statusRetweetView.snp_makeConstraints { (make) -> Void in
            make.left.right.equalTo(contentView)
            make.top.equalTo(statuseOriginaView.snp_bottom)

        }
        
    }
   
}
